package com.lx.factory;

import com.lx.model.JwtUser;
import com.lx.model.User;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 段誉
 * @create 2019-06-26 13:36
 */
public final class JwtUserFactory {
  private JwtUserFactory() {
  }

  public static JwtUser create(User user) {
    return new JwtUser(
            user.getId(),
            user.getUsername(),
            user.getPassword(),
            user.getEmail(),
            mapToGrantedAuthorities(user.getRoles()),
            user.getLastPasswordResetDate());
  }

  private static List<GrantedAuthority> mapToGrantedAuthorities(List<String> authorities) {

    return authorities.stream()
            .map(SimpleGrantedAuthority::new)
            .collect(Collectors.toList());
  }
}
